<template>
  <div class="kaocha-detail2">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/selection' }">干部选任</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/selection/kaocha' }">考察</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxnoborder">
      <div :style="'height:'+tableHeight+'px'">
        <el-scrollbar style="height: calc(100% + 16px);">
          <el-form ref="form"
            :model="kaochaForm"
            label-width="120px"
            label-position="right"
            :inline="true"
            style="margin:15px;">
            <el-form-item label="考察组成员">
                <el-input v-model="kaochaForm.name"></el-input>
            </el-form-item>
            <el-form-item label="考察时间">
                <el-date-picker type="date" placeholder="选择时间" v-model="kaochaForm.date" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item label="考察结果">
              <div class="sel-fl">
                <el-input-number v-model="kaochaForm.num" :min="0"></el-input-number>
              </div>
              <div class="sel-tit">人参加考察谈话。其中</div>
              <div class="sel-fl">
                <el-input-number v-model="kaochaForm.num2" :min="0"></el-input-number>
              </div>
              <div class="sel-tit">人同意。</div>
              <div class="sel-fl">
                <el-input-number v-model="kaochaForm.num3" :min="0"></el-input-number>
              </div>
              <div class="sel-tit">人不同意。</div>
              <div class="sel-fl">
                <el-input-number v-model="kaochaForm.num4" :min="0"></el-input-number>
              </div>
              <div class="sel-tit">人弃权。</div>
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item label="备注">
              <el-input v-model="kaochaForm.beizhu" style="width:800px"></el-input>
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item label="凡提四必情况">
               {{kaochaForm.ftsb}}
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item label="重要情况">
              <el-input v-model="kaochaForm.zyqk" style="width:800px"></el-input>
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item label="任前报告情况">
              <el-input v-model="kaochaForm.rqbg" style="width:800px"></el-input>
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item>
              <el-button type="primary" style="margin-left:120px" @click="exportdoc">生成考察素材模板</el-button>
              <el-button type="primary" @click="exportdoc2">导出档案查核表</el-button>
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item label="上传附件" prop="file">
              <el-upload
                class="upload-demo"
                :action="this.$http.defaults.baseURL+this.$http.defaults.selectionAPI +'api/gen/file/upload/'"
                :on-success="handleUploadSucc"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                :on-error='uploadError'
                :multiple="true"
                :file-list="fileList">
                <el-button type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">可同时上传多个文件</div>
              </el-upload>
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item label="审核人">
              <el-input v-model="kaochaForm.hcr"></el-input>
            </el-form-item>
            <el-form-item label="审核时间">
              <el-date-picker type="date" placeholder="选择时间" v-model="kaochaForm.date2" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item label="是否存在问题">
              <el-radio-group v-model="kaochaForm.problem">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="2">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="存在问题" v-if="kaochaForm.problem == 1">
              <el-input v-model="kaochaForm.hasproblem" style="width:800px"></el-input>
            </el-form-item>
            <div style="clear:both"></div>
            <el-form-item>
              <el-button type="primary" @click="onSubmit(1)" style="margin-left:120px">确认保存</el-button>
              <el-button type="success" @click="onSubmit(2)">考察完成通过提请审议</el-button>
              <el-button type="danger" @click="onSubmit(3)">考察未通过</el-button>
            </el-form-item>
          </el-form>
        </el-scrollbar>
      </div>
    </section>
  </div>
</template>

<script>
export default {
  name: 'kaocha-detail2',
  data () {
    return {
      tableHeight: 100,
      params: {},
      kaochaForm: {
        name: '',
        date: '',
        date2: '',
        num: 0,
        num2: 0,
        num3: 0,
        num4: 0,
        beizhu: '',
        ftsb: '',
        zyqk: '',
        rqbg: '',
        hcr: '',
        problem: '',
        hasproblem: ''
      },
      ftsb: '',
      files: [],
      fileList: [],
      selectData: {
        hjData: [
        ],
        szData: [
        ],
        cnData: [],
        yyData: []
      }
    }
  },
  methods: {
    exportdoc () {
      window.open(this.$http.defaults.baseURL + this.$http.defaults.selectionAPI + 'api/selection/document/exportPersonRegisterDoc?personId=' + this.params.busCandidatePersonId)
    },
    exportdoc2 () {
      let url = this.$http.defaults.selectionAPI + 'api/selection/document/exportCygbda?personId=' + this.params.busCandidatePersonId
      // console.log(url)
      this.$http.get(url).then(res => {
        if (res.data.success === true) {
          this.download(res.data.result)
        } else {
          this.$message({
            message: '数据获取失败',
            type: 'error'
          })
        }
      })
      // window.open(this.$http.defaults.baseURL + this.$http.defaults.selectionAPI + 'api/selection/document/exportDhjlb?batchId=' + id)
    },
    download (fileid) {
      var downloadElement = document.createElement('a')
      downloadElement.href = this.$http.defaults.baseURL + this.$http.defaults.selectionAPI + 'api/gen/file/download?id=' + fileid
      document.body.appendChild(downloadElement)
      downloadElement.click() // 点击下载
      document.body.removeChild(downloadElement) // 下载完成移除元素
    },
    loadSelectData () {
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/selection/auth/getUsersByRole?roletype=1`)
        .then(response => {
          if (response.data.success) {
            this.selectData.hjData = response.data.result
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/selection/auth/getUsersByRole?roletype=2`)
        .then(response => {
          if (response.data.success) {
            this.selectData.szData = response.data.result
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/selection/home/getCommonlyOpinions?step=4`)
        .then(response => {
          if (response.data.success) {
            this.selectData.yyData = response.data.result
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
    },
    handleUploadSucc (response, file, fileList) {
      console.log(response)
      if (response.success) {
        this.files.push(response.result[0])
        this.$message({
          message: '上传成功！' + response.message,
          type: 'success'
        })
      } else {
        this.$message({
          message: '上传失败:' + response.message,
          type: 'error'
        })
      }
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
      let fid = file.response.result[0].id
      let url = this.$http.defaults.selectionAPI + 'api/gen/file/remove'
      this.$http.get(url, {
        params: {
          id: fid
        }
      }).then(res => {
        console.log('删除成功:', res)
        if (res.data.success === true) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          })
        }
      })
    },
    uploadError (err) {
      console.log(err)
      this.$message({
        message: '上传失败:' + err,
        type: 'error'
      })
    },
    getKaochaData (id) {
      let that = this
      let url = this.$http.defaults.selectionAPI + 'api/selection/examination/getInfo'
      this.$http.get(url, {
        params: {
          personId: id
        }
      }).then(res => {
        console.log('kaochad2 data:', res)
        if (res.data.success === true) {
          let rs = res.data.result
          let arr = {}
          arr.name = rs.teamMembers
          arr.date = rs.examinationDate
          arr.date2 = rs.verifyTime
          arr.num = rs.noTotal
          arr.num2 = rs.noDisagreement
          arr.num3 = rs.noAgreed
          arr.num4 = rs.noAbstentions
          arr.beizhu = rs.remarks
          arr.ftsb = that.ftsb
          arr.zyqk = rs.importantSituation
          arr.rqbg = rs.report
          arr.hcr = rs.verifyUserName
          arr.problem = rs.hasProblem
          arr.hasproblem = rs.problemComment
          that.kaochaForm = arr
          // 获取上传文件列表
          let brr = []
          for (let i of rs.files) {
            brr.push({
              name: i.fileName + '.' + i.fileType,
              url: this.$http.defaults.baseURL + this.$http.defaults.selectionAPI + 'api/gen/file/download?id=' + i.id
            })
          }
          that.fileList = brr
        }
      })
    },
    getftsb (personid) {
      if (personid) {
        let url = this.$http.defaults.selectionAPI + 'api/selection/necessary/getPersonStepInfo'
        this.$http.post(url, {
          personId: personid
        }).then(response => {
          console.log('ftsb:', response)
          if (response.data.success) {
            let result1 = ''
            let result2 = ''
            let result3 = ''
            let result4 = ''
            if (response.data.result.stateCadreArchives === -1 || response.data.result.resultCadreArchives === '' || !response.data.result.resultSolicitingOpinions) {
              result1 = '无'
            } else {
              result1 = response.data.result.resultCadreArchives
            }
            if (response.data.result.statePersonalMatters === -1 || response.data.result.resultPersonalMatters === '' || !response.data.result.resultSolicitingOpinions) {
              result2 = '无'
            } else {
              result2 = response.data.result.resultPersonalMatters
            }
            if (response.data.result.stateSolicitingOpinions === -1 || response.data.result.resultSolicitingOpinions === '' || !response.data.result.resultSolicitingOpinions) {
              result3 = '无'
            } else {
              result3 = response.data.result.resultSolicitingOpinions
            }
            if (response.data.result.stateClueReporting === -1 || response.data.result.resultClueReporting === '' || !response.data.result.resultSolicitingOpinions) {
              result4 = '无'
            } else {
              result4 = response.data.result.resultClueReporting
            }
            this.kaochaForm.ftsb = '查核干部档案：' + result1 + '；查核有关事项：' + result2 + '；征求纪检监察部门意见：' + result3 + '；线索举报调查：' + result4
            this.ftsb = '查核干部档案：' + result1 + '；查核有关事项：' + result2 + '；征求纪检监察部门意见：' + result3 + '；线索举报调查：' + result4
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        }).catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
      }
    },
    onSubmit (req) {
      let that = this
      console.log('submit data', that.kaochaForm)
      if (that.kaochaForm.name === '') {
        that.$message({
          message: '考察组成员不能为空',
          type: 'warning'
        })
        return
      }
      if (that.kaochaForm.date === '') {
        that.$message({
          message: '请选择考察时间',
          type: 'warning'
        })
        return
      }
      if (that.kaochaForm.zyqk === '') {
        that.$message({
          message: '重要情况不能为空',
          type: 'warning'
        })
        return
      }
      if (that.kaochaForm.rqbg === '') {
        that.$message({
          message: '任前报告不能为空',
          type: 'warning'
        })
        return
      }
      if (that.kaochaForm.hcr === '') {
        that.$message({
          message: '审核人不能为空',
          type: 'warning'
        })
        return
      }
      if (that.kaochaForm.date2 === '') {
        that.$message({
          message: '请选择审核时间',
          type: 'warning'
        })
        return
      }
      if (that.kaochaForm.problem === '') {
        that.$message({
          message: '请选择是否存在问题',
          type: 'warning'
        })
        return
      }
      if (that.kaochaForm.problem === 1) {
        if (that.kaochaForm.hasproblem === '') {
          that.$message({
            message: '请填写存在问题',
            type: 'warning'
          })
          return
        }
      }
      // 获取上传文件id
      let arr = []
      for (let i of that.files) {
        arr.push(i.id)
      }
      console.log('文件上传ids:', arr)
      const loading = that.$loading({
        lock: true,
        text: '加载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      let url = that.$http.defaults.selectionAPI + 'api/selection/examination/saveInfo'
      that.$http.post(url, {
        busCandidatePersonId: that.params.busCandidatePersonId,
        teamMembers: that.kaochaForm.name,
        examinationDate: that.kaochaForm.date,
        noTotal: that.kaochaForm.num,
        noDisagreement: that.kaochaForm.num2,
        noAgreed: that.kaochaForm.num3,
        noAbstentions: that.kaochaForm.num4,
        remarks: that.kaochaForm.beizhu,
        importantSituation: that.kaochaForm.zyqk,
        report: that.kaochaForm.rqbg,
        verifyUserName: that.kaochaForm.hcr,
        verifyTime: that.kaochaForm.date2,
        hasProblem: that.kaochaForm.problem,
        problemComment: that.kaochaForm.hasproblem,
        stateExamination: req,
        files: arr
      }).then(res => {
        console.log('列表保存res:', res)
        if (res.data.success) {
          loading.close()
          that.$message({
            message: '保存成功',
            type: 'success'
          })
          that.$router.replace({
            name: 'kaocha',
            params: {
              step: 3
            }
          })
        } else {
          loading.close()
          that.$message({
            message: '保存失败:' + res.data.message,
            type: 'error'
          })
        }
      })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - 165
      })
    }
  },
  mounted () {
    this.loadSelectData()
    let params = this.$route.params
    console.log('kaochad2 mounted params:', params)
    this.params = params
    // 获取凡提四必数据
    this.getftsb(params.busCandidatePersonId)
    // 获取考察回显数据
    this.getKaochaData(params.busCandidatePersonId)

    this.tableresize(this)
    let _this = this
    window.onresize = function windowResize () {
      // 通过捕获系统的onresize事件触发我们需要执行的事件
      _this.tableresize(_this)
    }
  }
}
</script>
<style>
.kaocha-detail2 .wboxnoborder .el-table{
  border: #95B8E7 solid 1px;
}
.kaocha-detail2 .wboxnoborder .el-table th{
  font-weight: normal;
  background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  text-align: center;
  border-right: #95B8E7 solid 1px;
  border-bottom: #95B8E7 solid 1px;
  min-height: 30px;
  line-height:30px;
}
.kaocha-detail2 .wboxnoborder .el-table td, .el-table th{
  padding:0;
  font-size:var(--size14) !important;
  line-height: 20px;
  font-family: '宋体';
  text-align: center;
}
.kaocha-detail2 .el-table__row{
  height: 50px;
}
</style>
<style scoped>
.kaocha-detail2 {
  width: 100%;
}
.sel-tit {
  height: 32px;
  line-height: 32px;
  padding: 0 15px;
  box-sizing: border-box;
  float: left;
  color: #303133;
  text-align: right;
  font-size: 16px;
}
.sel-con {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding: 0 15px;
  box-sizing: border-box;
  float: left;
  color: #909399;
  border-bottom: 0.5px solid #909399;
  font-size: 14px;
}
.sel-fl {
  float: left;
}
.kaocha-detail2 .wboxnoborder {
  padding: 0;
  width: 100%;
  background-color: #fff;
  margin: 0 10px;
  overflow: hidden;
}
</style>
